<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\discount\FullCut;

/**
 * FullCutSearch represents the model behind the search form about `common\models\discount\FullCut`.
 */
class FullCutSearch extends FullCut
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['full_cut_id', 'c_obj', 'c_type', 'c_status'], 'integer'],
            [['title', 'begin_time', 'end_time', 'create_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = FullCut::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 20,
             ],
             'sort'=>[
                'defaultOrder'=>[
                    'create_time'=>SORT_DESC
                ]
             ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
       /*  $query->andFilterWhere([
            'full_cut_id' => $this->full_cut_id,
            'c_obj' => $this->c_obj,
            'c_type' => $this->c_type,
            'begin_time' => $this->begin_time,
            'end_time' => $this->end_time,
            'create_time' => $this->create_time,
        ]); */
        
        if($this->c_status >=0 ) {
            $query->andFilterWhere(['c_status' => $this->c_status]);
        }
        $query->andFilterWhere(['like', 'title', $this->title]);

        return $dataProvider;
    }
}
